package com.eostoken.eos_wallet.net.ssl;

import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class SSLSocketclient {
    //获取这个sslsocketfactory
    public static SSLSocketFactory getsslsocketfactory() {
        try {
            SSLContext sslcontext = SSLContext.getInstance("ssl");
            sslcontext.init(null, gettrustmanager(), new SecureRandom());
            return sslcontext.getSocketFactory();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    //获取trustmanager  x509trustmanager
    private static TrustManager[] gettrustmanager() {
        TrustManager[] trustallcerts = new TrustManager[]{
                new X509TrustManager() {
                    @Override
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    }

                    @Override
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {

                    }

                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[]{};
                    }
                }
        };
        return trustallcerts;
    }

    //获取hostnameverifier
    public static HostnameVerifier gethostnameverifier() {
        HostnameVerifier hostnameverifier = new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        return hostnameverifier;
    }
}
